﻿using System.Windows.Input;
using System.Windows.Threading;
using CSharpHelpers.IOs;

namespace CSharpHelpers.Wpfs
{
    class WpfEventHelper
    {
        /// <summary>
        /// 只能输入数字、字母以及退格、回车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void OnlyInsertNumAndLetter(object sender, KeyEventArgs e)
        {
            //屏蔽非法按键
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                || (e.Key >= Key.A && e.Key <= Key.Z)
                || e.Key == Key.Return || e.Key == Key.Back)
            {
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 只能输入数字、字母以及退格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void OnlyInsertNumAndLetterNoEnter(object sender, KeyEventArgs e)
        {
            //屏蔽非法按键
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                || (e.Key >= Key.A && e.Key <= Key.Z) || e.Key == Key.Back)
            {
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 只能输入数字、部分字母以及退格、回车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void OnlyInsertNumAndSomeLetter(object sender, KeyEventArgs e)
        {
            //屏蔽非法按键
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                || (e.Key >= Key.A && e.Key <= Key.I)
                || e.Key == Key.Return || e.Key == Key.Back)
            {
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 只能输入数字、小数点以及退格、回车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void OnlyInsertNumAndPoint(object sender, KeyEventArgs e)
        {
            //屏蔽非法按键
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Return || e.Key == Key.Back)
            {
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 只能输入数字以及退格、回车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void OnlyInsertNum(object sender, KeyEventArgs e)
        {
            //屏蔽非法按键
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Return || e.Key == Key.Back || e.Key == Key.Tab)
            {
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9)) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 屏蔽F5刷新功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void FobidF5Func(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F5)
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 屏蔽alt+f4功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static bool altDown = false;
        public static void FobidQuitFuncKeyDown(object sender, KeyEventArgs e)
        {
            if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
            {
                altDown = true;
            }
            else if (e.SystemKey == Key.F4 && altDown)
            {
                e.Handled = true;
            }
        }

        public static void FobidQuitFuncKeyUp(object sender, KeyEventArgs e)
        {
            if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
            {
                altDown = false;
            }
        }

        /// <summary>
        /// 捕捉System.Runtime.InteropServices.COMException (0x800401D0): 
        /// OpenClipboard 失败 (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))异常
        /// (剪贴板处理程序错误)，解决乱点datagrid抛出的COMException异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            var comException = e.Exception as System.Runtime.InteropServices.COMException;
            if (comException != null && comException.ErrorCode == -2147221040)
            {
                SystemLog.Error("COMException: " + comException.Message);
                e.Handled = true;
            }
        }
    }
}
